import {getuserUid, getRole} from './auth'
export const MessagePush = () => {
    let loop = () => {
        let run = async () => {
            let continueFunc = () => {
                setTimeout(() => {
                    run()
                }, 5e3)
            }
            let params = {
                role: getRole(),
                userUid: getuserUid()
            }
            if(!params.userUid){
                console.log('未登录状态')
                continueFunc()
                return
            }
            let response
            try {
                response = await HTTP('app/buser/getPushMsgList', params)
                if(response.code !== 200){
                    // TODO 错误消息汇报
                    console.log(response)
                    return
                }
                response.data.forEach(e => {
                    console.log('--push--', e)
                    let extra = {
                        type: '订单消息通知'
                    }
                    //参数
                    let options = {
                        //是否覆盖上一条推送
                        cover: false,
                        title: e.title
                    }
                    plus.push.createMessage(e.msgContent, extra, options)
                    // 下单
                    if(e.type == "1001"){
                        plus.audio.createPlayer({
                            autoplay: true,
                            volume: 1,
                            startTime: 0,
                            backgroundControl: false,
                            loop: false,
                            src: './audio/audio_order.mp3'
                        })
                    }
                })
                continueFunc()
            } catch (error) {
                continueFunc()
            }
        }
        run()
    }
    loop()
}
